<template>
  <el-dialog title="应用选择" :visible="visible" append-to-body @close="close">
    <el-container v-loading="loading" class="tab-container" width="550px">
      <el-main style="padding:0px;margin-top:0px;">
        <el-form
          ref="queryCondition"
          :model="queryCondition"
          inline
          class="header-search_form"
          @keyup.enter.native="handleQuery"
        >
          <el-form-item>
            <p-input v-model="queryCondition.name" placeholder="名称" />
          </el-form-item>
          <el-form-item>
            <p-input v-model="queryCondition.code" placeholder="编码" />
          </el-form-item>

          <el-form-item>
            <el-button
              v-permission="'cip:apiApp:query'"
              type="primary"
              icon="el-icon-search"
              @click="handleQuery"
            >查询</el-button>
          </el-form-item>
        </el-form>
        <div style="margin-top:10px;">
          <el-table
            :data="tableData"
            style="width: 100%;padding:0px;margin-top:10px;"
            stripe
            highlight-current-row
            border
            @sort-change="handleSortChange"
            @current-change="handleSelectionChange"
          >
            <el-table-column prop="name" label="名称" />
            <el-table-column prop="code" label="编码" />
          </el-table>
          <el-pagination
            :current-page="pageInfo.pageNum"
            :level1-sizes="constant.DEFAULT_PAGE_SIZE_ARR"
            :level1-size="pageInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageTotal"
            style="margin-top:10px;"
            @size-change="handlePageSize"
            @current-change="handlePage"
          />
        </div>
      </el-main>
    </el-container>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirm">确定</el-button>
      <el-button @click="close">关闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
import PInput from '@/components/popsoft/Query/PInput'

export default {
  name: 'userGroup-user',
  components: { PInput },
  data() {
    return {
      id: '',
      name: '',
      tableData: [],
      loading: false,
      pageTotal: 0,
      pageInfo: {
        pageNum: this.constant.DEFAULT_PAGE_NUM,
        pageSize: this.constant.DEFAULT_PAGE_SIZE
      },
      sortInfo: {
        sort_field: 'name',
        sort_sortType: 'ascending'
      },
      queryCondition: {

      },
      multipleSelection: [],
      visible: false
    }
  },
  methods: {
    show(id, name) {
      this.id = id
      this.name = name
      this.loadData(id)
      this.visible = true
    },
    handleSelectionChange(currentRow) {
      if (currentRow) {
        this.id = currentRow.id
        this.name = currentRow.name
      }
    },
    loadData() {
      this.loading = true
      const params = Object.assign(this.queryCondition, this.pageInfo, this.sortInfo)
      this.$api.cip.apiApp.page(params)
        .then(res => {
          this.tableData = res.data.records
          this.pageTotal = res.data.total
        }).finally(() => {
          this.loading = false
        })
    },
    handlePage(value) {
      this.pageInfo.pageNum = value
      this.loadData()
    },
    handlePageSize(value) {
      this.pageInfo.pageSize = value
      this.loadData()
    },
    handleQuery() {
      this.pageInfo.pageNum = 1
      this.loadData()
    },
    close() {
      this.visible = false
    },
    // 处理排序
    // eslint-disable-next-line no-unused-vars
    handleSortChange({ column, prop, order }) {
      this.sortInfo.sort_field = prop
      this.sortInfo.sort_sortType = order
      this.refresh()
    },
    confirm() {
      this.$emit('ok', this.id, this.name)
      this.visible = false
    }
  }
}
</script>

<style lang='scss' scoped>
.tab-container {
  padding: 15px;
  margin-left: 30px;
  width: 800px;
  .header {
    height: 50px !important;
    .header-search_form {
      height: 40px;
    }
  }
}
</style>
